package com.mrd.gtimp.base.action;

import com.mrd.constant.MsgConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.gtimp.base.entity.Answer;
import com.mrd.gtimp.base.entity.Question;
import com.mrd.gtimp.base.service.AnswerBiz;
import com.mrd.gtimp.base.service.QuestionBiz;
import com.mrd.util.LoginUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author qiul
 */
@Controller
@RequestMapping("/base")
public class AnswerAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private AnswerBiz answerBiz;

    @Resource
    private QuestionBiz quesBiz;

    @RequestMapping("/ans-list")
    public String list(Question ques) {
        try {
            if (ques != null) {
                if (StringUtils.isNotEmpty(ques.getId())) {
                    ques = quesBiz.findEntityById(ques.getId());
                }
            }
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return "/gtimp/base/ques/ansList";
    }

    @RequestMapping("/ans-save")
    @ResponseBody
    public Result save(Question ques, Answer ans) {
        Result msg;
        try {
            if (StringUtils.isNotEmpty(ques.getId())) {
                if (StringUtils.isEmpty(ans.getId())) {
                    ans.setId(null);
                    ans.setAtime(new Date());
                    ans.setIscheck("0");
                    ans.setQid(ques.getId());
                    ans.setUid(LoginUtil.getLoginUser().getId());
                    answerBiz.saveOrupdateEntity(ans);
                    // 已回复
                    ques = quesBiz.findEntityById(ques.getId());
                    ques.setIsanswer("1");
                    ques.setIscheck("1");
                    quesBiz.saveOrupdateEntity(ques);
                }
            }
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES, "成功");
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL, "失败");
        }
        return msg;
    }
}

